﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
using Helper;
namespace SchoolManagementSystem.Details
{
    //môn học
    public partial class SubjectUC : DetailUC
    {
        public SubjectUC()
        {
            InitializeComponent();
            this.ObjectName = "môn học";
            this.Code = "Subject";
            this.Type =new Subject();

            comboboxUC_Branch.Model = RegisterService.RegisterServices.BranchService;
            comboboxUC_Branch.Uc = new BranchUC();
            comboboxUC_Branch.ColumnMain = "Name";
            comboboxUC_Branch.ColumnDisplay = "Code,Name";

            //giao vien -  mon hoc
            masterMainUC1.Model = RegisterService.RegisterServices.SubjectTeacherService;
            masterMainUC1.DetailUCControl = new Subject_TeacherUC();

            //mon hoc - côt diem
            masterMainUC2.Model = RegisterService.RegisterServices.SubjectPointColumnService;
            masterMainUC2.DetailUCControl = new Subject_PointColumnUC();

        }
        String SubjectID = "1=1";
        protected override long DetailDisplay()
        {
            Subject data = new Subject();
            if (ID > 0)
            {
                data = (Subject)RegisterService.RegisterServices.SubjectService.GetByID(ID);
                SubjectID = data.ID.ToString();
            }
          
            txtcode.Text = data.Code;
            txtname.Text = data.Name;
            cbSemester.SelectedIndex = data.Semester;
            txtnumber.Text = ConvertObject.ToString(data.Factor);
            comboboxUC_Branch.ID = data.BranchID;
            ckactive.Checked = data.Active;
            txtnote.Text = data.Description;
            masterMainUC1.SearchString = " SubjectID=" + SubjectID + "";
            masterMainUC1.DetailUCControl.SearchString = " SubjectID=" + SubjectID + "";
            masterMainUC2.SearchString = " SubjectID=" + SubjectID + "";
            masterMainUC2.DetailUCControl.SearchString = " SubjectID=" + SubjectID + "";
            return ID;

        }

        protected override long Save()
        {
            Subject data = new Subject();
            if (ID > 0)
            {
                data = (Subject)RegisterService.RegisterServices.SubjectService.GetByID(ID);

            }

            data.Code = txtcode.Text;
            data.Name = txtname.Text;
            data.Semester = cbSemester.SelectedIndex;
            data.Factor = ConvertObject.ToInt(txtnumber.Text);
            data.Description = txtnote.Text;
            data.Active = ckactive.Checked;
            data.BranchID = comboboxUC_Branch.ID;
            data.Description = txtnote.Text;

            ID = RegisterService.RegisterServices.SubjectService.Save(data, ref ErrorMess);
            masterMainUC1.DetailUCControl.SearchString = ID.ToString();
            masterMainUC2.DetailUCControl.SearchString = ID.ToString();
            return ID;
        }


        public override bool Delete(long SelectedID)
        {

            if (SchoolManagementSystem.Controls.MessageControl.ShowMessageWithResult("Việc xóa môn học có thể ảnh hưởng đến việc hiển thị kết quả học tập của môn học này, bạn có đồng ý xóa ?", "Warning", MessageControl.Icon.Warning) == DialogResult.OK)
            {
                return RegisterService.RegisterServices.SubjectService.UpdateIsDelete(RegisterService.RegisterServices.SubjectService.GetByID(SelectedID));
            }
            return false;

          //  return RegisterService.RegisterServices.SubjectService.Delete(RegisterService.RegisterServices.SubjectService.GetByID(SelectedID));

        }

        private void SubjectUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage1.Controls);
        }




    }
}
